package com.customview.wzq.mdbehavior;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class MainActivity extends AppCompatActivity {

    private TextView tvItem1;
    private TextView tvItem2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tvItem1 = (TextView) this.findViewById(R.id.tv_main_item1);
        tvItem2 = (TextView) this.findViewById(R.id.tv_main_item2);

    }

    private void moveItem2() {
        Observable.create(new ObservableOnSubscribe<Integer>() {
            @Override
            public void subscribe(@NonNull ObservableEmitter<Integer> e) throws Exception {
                for(int i =0;i<100;i++){
                    Thread.sleep(100);
                    e.onNext(i);
                }
                e.onComplete();
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Integer>() {
                    @Override
                    public void accept(@NonNull Integer o) throws Exception {
                        ViewCompat.offsetTopAndBottom(tvItem1,10);
                    }
                });

    }

    public void start(View view) {
        moveItem2();
    }

    public void jump(View view) {
        startActivity(new Intent(this,Main2Activity.class));
    }

}
